import { _decorator, Component, Button, Label, Prefab, instantiate, Vec3, Sprite, Node } from 'cc';
import { Databus } from '../core/Databus';
import { UserAttribute } from '../core/UserAttribute';
import { ResourceManager } from '../framework/ResourceManager';
import { InterstitialAd } from '../platform/ad/InterstitialAd';
import { WxSdkManager } from '../platform/ad/WxSdkManager';

const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('RankingPage')
export class RankingPage extends Component {
  @property(Prefab)
  RankingItemLine: Prefab = null;

  private index = 0;
  public backButton: Button = null;
  private userList: UserAttribute[] = [];
  private maxRankingLen = 20;
  onLoad(): void {
    this.backButton = this.node.getChildByName('BackButton').getComponent(Button);
    this.backButton.node.on(Button.EventType.CLICK, this.backPage, this);
  }


  protected onEnable(): void {
   // console.log("555555555555555")
    WxSdkManager.toggleCustomCol33Ad(true)
  }

  protected onDisable(): void {
  //  console.log("777777777777777777")
    WxSdkManager.toggleCustomCol33Ad(false)
  }

  protected start(): void {
    this.userList.length = 0;
    for (const ua of databus.rankingList) {
      this.userList.push(ua);
    }
    this.updateUserHeaderTop3();
    InterstitialAd.Instance().show();
  }
  update(deltaTime: number) {
    this.updatePageUserHeader();
  }

  public updatePageUserHeader() {
    if (this.index >= this.maxRankingLen) {
      return;
    }
    if (!this.userList) {
      return;
    }
    if (this.index >= this.userList.length) {
      return;
    }
    const content = this.node.getChildByName('RankingLine').getChildByName('ScrollView').getChildByName('view').getChildByName('content');
    const pos = new Vec3(-250, 0, 0);
    const user = this.userList[this.index];
    const itemNode: Node = instantiate(this.RankingItemLine);
    itemNode.name = `item${this.index+1}`;
    itemNode.getChildByName('RankLabel').getComponent(Label).string = `${this.index+1}`;
    pos.y = - this.index * 100 - 10;
    itemNode.setPosition(pos);
    itemNode.setParent(content);

    itemNode.getChildByName('UserName').getComponent(Label).string = user.showNickName();
    itemNode.getChildByName('UserID').getComponent(Label).string = `id${user.showUserId()}`;
    itemNode.getChildByName('UserLevelName').getComponent(Label).string = user.playLevel.playLevelName();
    itemNode.getChildByName('UserLevelScore').getComponent(Label).string = `(${user.playLevel.playLevel}级)`;

    ResourceManager.Instance().setNodeImageSpriteFrame(itemNode.getChildByName('UserHeader'), user.avatarUrl);

    this.index += 1;
  }
  public updateUserHeaderTop3() {
    const content = this.node.getChildByName('RankingLine').getChildByName('ScrollView').getChildByName('view').getChildByName('content');
    for (var i=0; i<this.userList.length && i<3; i++) {
      const user = this.userList[i];
      var itemNode: Node = null;
      itemNode = content.getChildByName(`item${i+1}`);
      itemNode.getChildByName('UserName').getComponent(Label).string = user.showNickName();
      itemNode.getChildByName('UserID').getComponent(Label).string = `id${user.showUserId()}`;
      itemNode.getChildByName('UserLevelName').getComponent(Label).string = user.playLevel.playLevelName();
      itemNode.getChildByName('UserLevelScore').getComponent(Label).string = `(${user.playLevel.playLevel}级)`;
      ResourceManager.Instance().setNodeImageSpriteFrame(itemNode.getChildByName('UserHeader'), user.avatarUrl);
    }
    this.index = 3;
    var myRankIndex = 1001;
    for (var i=0; i<this.userList.length; i++) {
      if (this.userList[i].userId === databus.selfInfo.userId) {
        myRankIndex = i + 1;
      }
    }
    const MyRankNode = this.node.getChildByName('MyLine');
    MyRankNode.getChildByName('UserName').getComponent(Label).string = databus.selfInfo.showNickName();
    MyRankNode.getChildByName('UserLevelName').getComponent(Label).string = databus.selfInfo.playLevel.playLevelName();
    MyRankNode.getChildByName('UserLevelScore').getComponent(Label).string = `(${databus.selfInfo.playLevel.playLevel}级)`;
    MyRankNode.getChildByName('RankLabel').getComponent(Label).string = `${myRankIndex}`;
    if (myRankIndex > this.maxRankingLen) {
      MyRankNode.getChildByName('RankLabel').getComponent(Label).string = '未上榜';
    }
    MyRankNode.getChildByName('UserHeader').getComponent(Sprite).spriteFrame = ResourceManager.Instance().getImageSpriteFrame(ResourceManager.SelfUserHeaderImage);
  }
  public backPage() {
    databus.sceneStack.backScene();
  }
}